package com.music.service.impl;

import com.music.dao.SongMapper;
import com.music.dao.UserPlaylistMapper;
import com.music.entity.PlaylistVo;
import com.music.entity.Song;
import com.music.entity.User;
import com.music.entity.UserPlayList;
import com.music.service.PlaylistService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 名一
 * @ClassName PlaylistServiceImpl
 * @description: TODO
 * @datetime 2022年 07月 08日 14:00
 * @version: 1.0
 */
@Service
@Slf4j
public class PlaylistServiceImpl implements PlaylistService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SongMapper songMapper;

    @Autowired
    private HttpServletRequest request;

    @Override
    public List<PlaylistVo> getPlaylistByUser() {
        List<PlaylistVo> playlistVoList = new ArrayList<>();

        String userId = ((User) request.getSession().getAttribute("user")).getUserId();

        if(userId != null){
            String downUrl = "../downloadFileFromQiniu?fileName=";
            List range = redisTemplate.opsForList().range(userId, 0, -1); // 取出用户对应的播放列表对应的歌曲id集合
            if(!range.isEmpty()){
                List<Song> songList = songMapper.selectBatch(range); // 从数据库取出对应歌曲信息
                songList.forEach(song->{
                    PlaylistVo playlistVo = new PlaylistVo();
                    playlistVo.setName(song.getSongName());
                    playlistVo.setSinger(song.getSinger().getSingerName());
                    playlistVo.setAlbum(song.getAlbum().getAlbumName());
                    playlistVo.setTime(song.getSongDuration());
                    playlistVo.setLink_url(song.getSongAudioSite());
                    playlistVo.setCover(new StringBuffer().append(downUrl).append(song.getSongImage()).toString());
                    playlistVo.setLink_lrc(new StringBuffer().append(downUrl).append(song.getSongLyricSite()).toString());
                    playlistVoList.add(playlistVo);
                });
            }
        }
        return playlistVoList;
    }

    @Override
    public void addSong(Integer songId) {

        String userId = ((User) request.getSession().getAttribute("user")).getUserId();

         // 用户编号 即redis中播放列表编号

            if(redisTemplate.opsForList().size(userId) > 20){
                if(redisTemplate.opsForList().indexOf(userId,songId)>= 0){
                    redisTemplate.opsForList().remove(userId,1,songId);
                }
                redisTemplate.opsForList().leftPush(userId,songId);
                redisTemplate.opsForList().rightPop(userId);
            }else{
                redisTemplate.opsForList().leftPush(userId,songId);
            }
    }

    @Override
    public void addSonglist(List<Integer> songIdList) {
        String userId = ((User) request.getSession().getAttribute("user")).getUserId();

        if(redisTemplate.hasKey(userId)){
            redisTemplate.delete(userId);
        }
        redisTemplate.opsForList().rightPushAll(userId,songIdList);
    }

}
